package edu.bistu.sim.xwy.blog.Controller;

import edu.bistu.sim.xwy.blog.Service.*;
import edu.bistu.sim.xwy.blog.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class BusGoodsController {
    @Autowired
    private BusGoodsSevice busGoodsService;
    @Autowired
    private BusUserService busUserService;

    @GetMapping("/AllGoods")
    public List<BusGoodsInfo> AllGoodsById(int bus_user_id){
        List<BusGoodsInfo> busGoodsInfos =new ArrayList<>();
        busGoodsInfos=busGoodsService.AllGoodsById(bus_user_id);
        for (int i=0;i<busGoodsInfos.size();i++){
            busGoodsInfos.get(i).setBusUser_goods_id(null);
        }
        return busGoodsInfos;
    }
    @RequestMapping("/findGoodsByName")
    public List <BusGoodsInfo> findGoodsByName(@Param("bus_user_name") String bus_user_name){
        System.out.println("名称为"+bus_user_name);
        List<BusGoodsInfo> busGoodsInfos =busGoodsService.findGoodsByName(bus_user_name);
        System.out.println("名称为"+bus_user_name);
      return busGoodsInfos;
    };
    @PostMapping("/addGoods")
    public BusGoodsInfo addCom(@Param("user_name")String user_name,@Param("goods_content")String goods_content,
                               @Param("goods_id")int goods_id,@Param("goods_name")String goods_name,@Param("goods_priece") String goods_priece,
                               @Param("goods_type")String goods_type){
        BusGoodsInfo busGoodsInfo=new BusGoodsInfo();
        BusUser busUser=busUserService.getOne(busUserService.findUserByName(user_name).getBus_user_id());

        busGoodsInfo.setGoods_id(goods_id);
        busGoodsInfo.setBusUser_goods_id(busUser);
        busGoodsInfo.setGoods_content(goods_content);
        busGoodsInfo.setGoods_name(goods_name);
        busGoodsInfo.setGoods_priece(goods_priece);
        busGoodsInfo.setGoods_type(goods_type);



        return busGoodsInfo;
    }


}
